var util = require("../../utils/util.js");
// pages/sendBox/sendBox.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderInfo: {},
    orderDetailList: {},
    hasNum: '',
    recipient_id: "",
    orderDetailInfoTracking_infoData: {},
    markers: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.recipient_id) {
      this.setData({
        recipient_id: options.recipient_id,
      })
    }
    var that = this;
    wx.setNavigationBarTitle({
      title: '订单详情',
    })
    // 获取本地数据-用户信息
    wx.getStorage({
      key: 'userInfo',
      // 能获取到则显示用户信息，并保持登录状态，不能就什么也不做
      success: (res) => {
        wx.hideLoading();
        this.setData({
          userInfo: res.data.userInfo,
        })
      }
    });
    this.setData({
      order_id: options.order_id,
    })
    var _order_id = options.order_id;
    var _user_id = wx.getStorageSync("userInfo").userInfo.uid;

    // 查询订单信息
    const requestGetOrderDetail = wx.request({
      url: "https://boxapi.supergk.com/server/order/going_order_detail",
      data: {
        order_id: _order_id,
        user_id: _user_id
      },
      success: function (res) {
        // 获取当前箱子的位置
        that.setData({
          latitude: res.data.data.box_info.latitude,
          longitude: res.data.data.box_info.longitude,
        })
        console.log(res.data.data.box_info.latitude);
        var _marks = [{
          id: res.data.data.box_info.id,
          latitude: res.data.data.box_info.latitude,
          longitude: res.data.data.box_info.longitude,
          name: "当前箱子位置",
          // latitude: 31.681013,
          // longitude: 120.321515,
          iconPath: "../../images/but_mapbox.png",
          title: '智能箱子',
          width: "45",
          height: "50",
        }]

        var mapCtx = wx.createMapContext("myMap")
        var latitude = res.data.data.box_info.latitude, longitude = res.data.data.box_info.longitude;

        //获取当前地图的中心经纬度
        mapCtx.includePoints({
          padding: [500],//地图比例
          points: [{
            latitude: latitude,
            longitude: longitude
          }]
        })
        mapCtx.translateMarker({
          markerId: 0,
          autoRotate: true,
          duration: 1000,
          destination: {
            latitude: latitude,
            longitude: longitude,
          },
          animationEnd() {
            console.log('animation end')
          }
        })

        console.log(_marks);
        that.setData({
          markers: _marks
        })
        console.log(res);
        if (res.data.code == 0) {
          that.setData({
            orderDetailInfo: res.data.data,
          })
          if (res.data.data.order_detail.tracking_number == "") {
            that.setData({
              hasNum: "0",
            })
          } else {
            that.setData({
              hasNum: "1",
              tracking_number: res.data.data.order_detail.tracking_number
            })
          }
          if (!res.data.data.tracking_info.length) {
            console.log("快递信息列表")
            console.log(res.data.data.tracking_info.data)
            that.setData({
              orderDetailInfoTracking_infoData: res.data.data.tracking_info.data
            })
          }
        }
      }
    })
  },

  // 修改快递单号
  changeLogisticsNum: function () {
    this.setData({
      hasNum: "0",
    })
    console.log(this.data.hasNum);
  },

  logisticsNumInput: function (e) {
    console.log(e.detail.value)
    this.setData({
      logisticsNumNow: e.detail.value
    })
  },
  // 设置快递单号
  queryLogisticsNum: function (e) {
    var that = this;
    const dataset = e.currentTarget.dataset;
    var order_id = dataset.order_id;
    var logisticsNumNow = this.data.logisticsNumNow;
    var uid = wx.getStorageSync("userInfo").userInfo.uid;
    const requestGetqueryLogisticsNumInfo = wx.request({
      url: "https://boxapi.supergk.com/server/user/update_order_tracking_number",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: util.json2Form({ tracking_number: logisticsNumNow, uid: uid, order_id: order_id }),
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          if (res.data.data.tracking_info != "") {
            that.setData({
              orderDetailInfoTracking_infoData: res.data.data.tracking_info.data
            })
          }
          that.setData({
            hasNum: "1",
            tracking_number: logisticsNumNow,
          })
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // this.mapCtx = wx.createMapContext('myMap');
    // this.mapCtx.moveToLocation()
    // var mapCtx = wx.createMapContext("myMap")
    // console.log(this.data.latitude);
    // console.log(this.data.longitude);
    // var latitude = this.data.latitude, longitude = this.data.longitude;
    // // mapCtx.getCenterLocation({
    //   success: function (res) {
    //     latitude = res.latitude;
    //     longitude = res.longitude;
    //   }
    // }) 
    //获取当前地图的中心经纬度
    // mapCtx.includePoints({
    //   padding: [10],
    //   points: [{
    //     latitude: latitude,
    //     longitude: longitude
    //   }]
    // })
    // mapCtx.translateMarker({
    //   markerId: 0,
    //   autoRotate: true,
    //   duration: 1000,
    //   destination: {
    //     latitude: latitude,
    //     longitude: longitude,
    //   },
    //   animationEnd() {
    //     console.log('animation end')
    //   }
    // })
    // mapCtx.openLocation({
    //   latitude: 31.681013,
    //   longitude: 120.321515,
    //   scale: 28
    // })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 点击结束订单
  movetoOrderPay: function (e) {
    var that = this;
    const dataset = e.currentTarget.dataset;
    var order_id = dataset.order_id;

    var _uid = wx.getStorageSync("userInfo").userInfo.uid;
    const requestEndOrder = wx.request({
      url: "https://boxapi.supergk.com/server/order/end_order",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: util.json2Form({ order_id: order_id, user_id: _uid }),
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          console.log(res.data.msg);


          // 开箱成功后  请求 进行中订单接口 刷新进行中订单数
          //获取正在进行中的订单个数
          var _uid = wx.getStorageSync("userInfo").userInfo.uid;
          const requestorderingNum = wx.request({
            url: 'https://boxapi.supergk.com/server/order/query_going_order_count',
            data: {
              uid: _uid
            },
            header: {
              'content-type': 'application/json'
            },
            method: "GET",
            success: function (res) {
              if (res.data.code == "0") {
                that.setData({
                  orderingNum: res.data.data.count,
                })
              } else {
                console.log();
              }
            }
          })//获取正在进行中的订单个数 結束

          wx.navigateTo({
            url: '../my/orderPay/orderPay?id=' + order_id,
          })
        } else {
          console.log(res.data.msg)
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false
          })
        }
      }
    })

  },
  movetoInformant: function (e) {
    const dataset = e.currentTarget.dataset;
    var box_id = dataset.box_id;
    var box_bn = dataset.box_bn;
    wx.navigateTo({
      url: '../informant/informant?box_bn=' + box_bn + '&box_id=' + box_id,
    })
  },
  movetoMyrecipients: function (e) {
    const dataset = e.currentTarget.dataset;
    var order_id = dataset.orderid;
    var order_bn = dataset.orderbn;
    wx.navigateTo({
      url: '../my/myrecipients/myrecipients?order_id=' + order_id + '&order_bn=' + order_bn + '&fromPage=sendBox'
    })
  }
})